<?php

	namespace view;

		require_once ("view/HTMLheaderView.php");
		require_once ("view/HTMLfooterView.php");

		class HTMLPageView
		{
			private static $userName = "Name";
			private static $userPassword = "Password";
			private static $checkBox = "CheckBox";
			private static $logInButton = "Button";
			private static $logOutButton = "Button";
			private static $openSite = "openSite";
			private static $closeSite = "closeSite";
			
			private $status = "Inte inloggad";
			private $returningName;
			private $infoMessage = "";
			private $message = "";
			private $title = "Logga in";

			public function __construct($returningName)
			{
				$this->returningName = $returningName;
				$this->pageHeaderView = new \view\HTMLheaderView();
				$this->pageHeaderView->getPageHeader();			
				$this->pageFooterView = new \view\HTMLfooterView();
				$this->pageFooterView->getPageFooter();
			}

			public function getPage($message, $status)
			{

				$this->message = $message;
				$this->status = $status;

				return $this->pageHeaderView->getPageHeader() . "<div id=\"content\"><div id='statusString'>" . $this->status . "</div>
				<form id=\"form\" name=\"form\" method=\"post\" action=\"?" . self::$openSite . "\">
				<fieldset><legend>" . $this->title . "<br>" . $this->infoMessage . "<br>" . $this->message . "</legend>
				<label for=\"name\">Namn:</label>
				<input type=\"text\" id=\"name\" name=\" " . self::$userName . "\" value=\"" . $this->returningName . "\"/>
				<label for=\"password\">Lösen</label>
				<input type=\"password\" id=\"password\" name=\" " . self::$userPassword . "\" value=\"\" />
				<label for=\"LoggedInStatus\">Fortsätt vara inloggad: </label>
				<input type=\"checkbox\" id=\"LoggedInStatus\" name=\" " . self::$checkBox . "\" value=\"yes\"/></fieldset>
				<fieldset><button  type=\"submit\" name=\" " 
				. self::$logInButton . "\">Logga in</button>
				</fieldset></form></div>" . $this->pageFooterView->getPageFooter();
			}

			public function getLoggedInPage($message, $status, $returningName)
			{

				$this->message = $message;
				$this->status = $status;
				$this->returningName = $returningName;
				$this->title = "";

				return $this->pageHeaderView->getPageHeader() . "<div id=\"content\"><h3 id='statusString'>" . $this->status . "</h3>
				
				<header><h1>" . $this->infoMessage . "<br>" . $this->message . "!</h1></header>
				
				<form id=\"form\" name=\"form\" method=\"post\" action=\"?" . self::$closeSite . "\">
				<div><br><br><fieldset><button  type=\"submit\" name=\" " 
				. self::$logOutButton . "\">Logga ut</button>
				</fieldset>
				</form>
				
				</fieldset></form></div>" . $this->pageFooterView->getPageFooter();

			}

			//returns boolean true if "openSite" is in URL after user submitted form
			public function userWantsToLogIn()
			{
				if (isset($_GET[self::$openSite]))
				{
					return true;
				}
				return false;
			}
			
			//returns boolean 
			public function userWantsToLogOut()
			{
				if (isset($_GET[self::$closeSite]))
				{
	
					return true;
				}
				return false;
			}

			//returns new user with user input if possible
			//otherwhise a message with catched information
			public function getNewUser()
			{
				$name = $this->cleanInput(self::$userName);
				$password = $this->cleanInput(self::$userPassword);
				if (strlen($name) > 3) 
				
				{
					$this->returningName = $name;
				}
				try
				{
					return new \model\User($name, $password);
				}
				catch(\Exception $e)
				{
					$this->infoMessage = $e->getMessage();
					return false;
				}
			}
			
			


			//returns input after sanitation
			//borrowed functions, cleanInput &  sanitizeInput from
			//https://coursepress.lnu.se/kurs/webbutveckling-med-php/exempelsida/en-controller-och-indata/
			private function cleanInput($inputValue)
			{
				if (isset($_POST[$inputValue]) == false)
				{
					return "";
				}
				return $this->sanitizeInput($_POST[$inputValue]);
			}
			
			private function sanitizeInput($input)
			{
				$temp = trim($input);
				return filter_var($temp, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
			}

		}
